//
//  ViewController.m
//  获取相册图片第三方
//
//  Created by apple on 16/4/19.
//  Copyright © 2016年 m14a.cn. All rights reserved.
//  参考第三方框架的案例来做

#import "ViewController.h"
#import <CTAssetsPickerController/CTAssetsPickerController.h>

#define Margin 5

#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height

@interface ViewController () <CTAssetsPickerControllerDelegate>

@end

@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
        dispatch_async(dispatch_get_main_queue(), ^{
            // 如果不是授权状态,直接返回
            if (status != PHAuthorizationStatusAuthorized) return;
            
            CTAssetsPickerController *picker = [[CTAssetsPickerController alloc] init];
            
            picker.delegate = self;
            
            [self presentViewController:picker animated:YES completion:nil];
        });
    }];
}

#pragma mark - CTAssetsPickerControllerDelegate方法

// implement should select asset delegate
- (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldSelectAsset:(PHAsset *)asset
{
    NSInteger max = 4;
    
   
    if (picker.selectedAssets.count >= max)
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"最多选择%zd张图片", max] preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action =
        [UIAlertAction actionWithTitle:@"OK"
                                 style:UIAlertActionStyleDefault
                               handler:nil];
        
        [alert addAction:action];
        // 这里不能使用self来modal别的控制器，因为此时self.view不在window上
        [picker presentViewController:alert animated:YES completion:nil];
        
    }

    return (picker.selectedAssets.count < max);
}

- (void)assetsPickerController:(CTAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
{
    // 关闭图片选择界面
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    // 基本配置
    CGFloat scale = [UIScreen mainScreen].scale;
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.resizeMode = PHImageRequestOptionsResizeModeExact;
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    
    CGFloat width = (ScreenW - Margin * 2) / 3;
    CGFloat height = width;
    
    // 遍历选择的所有图片
    for (NSInteger i = 0; i < assets.count; i++) {
        PHAsset *asset = assets[i];
        CGSize size = CGSizeMake(asset.pixelWidth / scale, asset.pixelHeight / scale);
        
        // 获取图片,去发请求，请图片
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
           
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.image = result;
            
            imageView.frame = CGRectMake((i % 3) * (width + Margin), (i / 3) * (height + Margin), width, height);
            
            [self.view addSubview:imageView];
            
        }];
    }
}


@end
